/*******************************************************************************
 * Copyright (c) 2008 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.classdiagram.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.classdiagram.tabbedproperties.composite.BehaviorParametersComposite;
import com.cea.papyrus.umlutils.Element;

/**
 * Section to describe behavior parameters
 */
public class BehaviorParametersSection extends PropertyViewSection {

	/** Composite to describe the parameter of the element*/
	private BehaviorParametersComposite paramsComposite;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void refresh() {
		paramsComposite.refresh();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		// set a grid Layout
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.makeColumnsEqualWidth = true;
		layout.marginWidth = 0; // remove additional spaces
		layout.marginHeight = 0; // remove additional spaces
		parent.setLayout(layout);

		GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWT.FILL;

		// table for Parameters
		paramsComposite = new BehaviorParametersComposite();
		paramsComposite.createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
		paramsComposite.setLayoutData(data);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		if (input instanceof IUMLElementEditPart) {
			org.eclipse.uml2.uml.Element element = ((IUMLElementEditPart) input).getUmlElement();
			if(element instanceof org.eclipse.uml2.uml.Behavior) {
				Element utilElement = new Element((org.eclipse.uml2.uml.Behavior)element);
				paramsComposite.setElement(utilElement);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dispose(){
		if(paramsComposite!=null) {
			paramsComposite.dispose();
		}
		super.dispose();
	}
}
